package com.jsmile.cloud.tradecenter.order.repo;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsmile.cloud.tradecenter.api.req.ReqOrderQuery;
import com.jsmile.cloud.tradecenter.express.repo.OrderExpressSearchRepository;
import com.jsmile.cloud.tradecenter.order.dao.OrderDao;
import com.jsmile.cloud.tradecenter.order.model.Order;
import com.jsmile.mall.api.enums.JSmileCacheEnum;
import com.jsmile.mall.api.exception.ExceptionKit;

import cn.hutool.core.collection.CollectionUtil;
import lombok.AllArgsConstructor;

@Component
@AllArgsConstructor
@Transactional(readOnly = true)
public class OrderSearchRepository extends ServiceImpl<OrderDao, Order> {

    private final OrderProductSearchRepository orderProductSearchRepository;
    private final OrderDiscountSearchRepository orderDiscountSearchRepository;
    private final OrderAddressSearchRepository orderAddressSearchRepository;
    private final OrderExpressSearchRepository orderExpressSearchRepository;

    @Cacheable(value = JSmileCacheEnum.JSmileCacheName.oneMonth, key = "'thirdOrderNo_'+#orderNo")
    public Order findByThirdOrderNo(String thirdOrderNo) {
        ExceptionKit.checkNotNull(thirdOrderNo, "第三方订单编号不能为空!!");
        return this.baseMapper.selectOne(new QueryWrapper<Order>().lambda().eq(Order::getThirdOrderNo, thirdOrderNo));
    }

    /**
     * 只查询订单信息
     */
    @Cacheable(value = JSmileCacheEnum.JSmileCacheName.oneMonth, key = "'orderNo_simple_'+#orderNo")
    public Order findBySimpleOrderNo(long orderNo) {
        Order order = this.baseMapper.selectById(orderNo);
        ExceptionKit.checkNotNull(order, "订单不存在!");
        return order;
    }

    public List<Order> findBySimpleOrderNos(List<Long> orderNos) {
        ExceptionKit.checkArgument(CollectionUtil.isEmpty(orderNos), "订单数组不能为空!");
        return this.baseMapper.selectBatchIds(orderNos);
    }

    public List<Order> findByOrderNos(List<Long> orderNos) {
        ExceptionKit.checkArgument(CollectionUtil.isEmpty(orderNos), "订单数组不能为空!");
        return orderNos.stream().map(e -> {
            return findByOrderNo(e);
        }).collect(Collectors.toList());
    }

    /**
     * 查询订单等其它信息
     */
    @Cacheable(value = JSmileCacheEnum.JSmileCacheName.oneMonth, key = "'orderNo_'+#orderNo")
    public Order findByOrderNo(long orderNo) {
        Order order = this.baseMapper.selectById(orderNo);
        ExceptionKit.checkNotNull(order, "订单不存在!");
        order.setOrderAddress(orderAddressSearchRepository.findOneByOrderNo(orderNo));
        order.setOrderDiscounts(orderDiscountSearchRepository.findByOrderNo(orderNo));
        order.setOrderProducts(orderProductSearchRepository.findByOrderNo(orderNo));
        return order;
    }

    public IPage<Order> findAll(ReqOrderQuery reqOrderQuery) {
        IPage<Order> orders = this.baseMapper.findAll(reqOrderQuery.getPage(), reqOrderQuery);
        if (!CollectionUtils.isEmpty(orders.getRecords())) {
            orders.getRecords().forEach(x -> {
                x.setOrderProducts(orderProductSearchRepository.findByOrderNo(x.getId()));
                x.setOrderExpresses(orderExpressSearchRepository.findByOrderNo(x.getId().toString()));
            });
        }
        return orders;
    }
}
